//
//  EMToastControl.swift
//  feiyu444
//
//  Created by hanweiguang on 2018/11/28.
//  Copyright © 2018年 hanweiguang. All rights reserved.
//

import UIKit
import Foundation

protocol EMToastControlDelegate: NSObjectProtocol {
    func toastDidHide(toastControl: EMToastControl?)
}

class EMToastControl: UIControl {
    
    let toastLabel: UILabel = UILabel.init()
    weak var delegate: EMToastControlDelegate?
    
    
    private var message: String?
    private var leftMinMargin: CGFloat?
    private var topMargin: CGFloat?
    
    private let toastLabelTopMargin: CGFloat = CGFloat(10)
    private let toastLabelLeftMargin: CGFloat = CGFloat(5)
    private let toastLabelLineSpacing: CGFloat = CGFloat(6)
    
    
    deinit {
        print(self.description + "消失了")
    }
    
    init(message: String, leftMinMargin:CGFloat, topMargin: CGFloat) {
        super.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        self.message = message
        self.leftMinMargin = leftMinMargin
        self.topMargin = topMargin
        self.configSetting()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
     * 相关数据配置
     */
    func configSetting() {
        self.backgroundColor = UIColor.init(white: 0, alpha: 1)
        self.layer.masksToBounds = true
        self.layer.cornerRadius = 4.0
        self.alpha = 0.0
        
        self.toastLabel.textAlignment = .center
        self.toastLabel.font = UIFont.systemFont(ofSize: 14)
        self.toastLabel.numberOfLines = 0
        
        let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle.init()
        paragraphStyle.lineSpacing = toastLabelLineSpacing
        paragraphStyle.alignment = .center
        paragraphStyle.lineBreakMode = .byWordWrapping
        
        let muString: NSMutableAttributedString = NSMutableAttributedString.init(string: self.message!, attributes: [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14),NSAttributedString.Key.foregroundColor:UIColor.white,NSAttributedString.Key.paragraphStyle:paragraphStyle])
        
        /** 文本最大宽度*/
        let tmpTextMaxWidth: CGFloat = UIScreen.main.bounds.size.width - self.leftMinMargin! * 2 - toastLabelLeftMargin * 2
        /** 多属性文本size*/
        let tmpTextSize: CGSize = muString.boundingRect(with: CGSize.init(width: tmpTextMaxWidth, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, context: nil).size
        
        /** 单行文本高度*/
        let singleLineTextHeigh: CGFloat = self.toastLabel.font.lineHeight
        
        var selfHeight: CGFloat = tmpTextSize.height + toastLabelTopMargin * 2
        if tmpTextSize.height/singleLineTextHeigh > 1.0 && tmpTextSize.height/singleLineTextHeigh < 2.0 {
            selfHeight-=toastLabelLineSpacing
        }
        
        let leftMargin: CGFloat = (UIScreen.main.bounds.size.width - tmpTextSize.width - toastLabelLeftMargin * 2)/2.0
        
        self.frame = CGRect.init(x: leftMargin, y: self.topMargin!, width: tmpTextSize.width + toastLabelLeftMargin * 2, height: selfHeight)
        self.toastLabel.frame = CGRect.init(x: toastLabelLeftMargin, y: toastLabelTopMargin, width: tmpTextSize.width, height: tmpTextSize.height)
        
        self.toastLabel.attributedText = muString
        
    }
    
    /**
     * 显示
     */
    func show() {
        UIApplication.shared.keyWindow?.endEditing(true) /** 避免键盘的遮挡*/
        UIApplication.shared.keyWindow?.addSubview(self)
        self.addSubview(self.toastLabel)
        
        UIView.animate(withDuration: 0.3,
                       animations: {[weak self] in
                        self?.alpha = 1.0
        }) { (complete) in
            DispatchQueue.global().async {
                sleep(3)
                DispatchQueue.main.async {[weak self] in
                    self?.hide()
                }
            }
        }
    }
    
    /**
     * 隐藏
     */
    func hide() {
        
        UIView.animate(withDuration: 0.3,
                       animations: {[weak self] in
                        self?.alpha = 0.0
        }) {[weak self] (complete) in
            self?.removeFromSuperview()
            if self?.delegate != nil {
                self?.delegate?.toastDidHide(toastControl: self)
            }
        }
    }
}
